package ModelCreator;

import me.prettyprint.cassandra.serializers.StringSerializer;
import me.prettyprint.cassandra.service.ThriftKsDef;
import me.prettyprint.hector.api.Cluster;
import me.prettyprint.hector.api.Keyspace;
import me.prettyprint.hector.api.ddl.ColumnFamilyDefinition;
import me.prettyprint.hector.api.ddl.ComparatorType;
import me.prettyprint.hector.api.ddl.KeyspaceDefinition;
import me.prettyprint.hector.api.factory.HFactory;
import me.prettyprint.hector.api.mutation.Mutator;

import java.util.Arrays;

public class WorkflowEvents {

    private Cluster cluster;
    private ColumnFamilyDefinition columnFamilyDefinition;
    private KeyspaceDefinition newKeyspace;
    private Keyspace keyspace;


    public WorkflowEvents() {
        cluster = HFactory.getOrCreateCluster(ModelConfig.CASSANDRA_CLUSTER, ModelConfig.CLUSTER_PORT);
        columnFamilyDefinition = HFactory.createColumnFamilyDefinition(ModelConfig.KEYSPACE, ModelConfig.WORKFLOW_EVENT_COLUMN_FAMILY, ComparatorType.BYTESTYPE);
        if (cluster.describeKeyspace(ModelConfig.KEYSPACE) == null) {
            newKeyspace = HFactory.createKeyspaceDefinition(ModelConfig.KEYSPACE, ThriftKsDef.DEF_STRATEGY_CLASS, 1, Arrays.asList(columnFamilyDefinition));
            cluster.addKeyspace(newKeyspace, true);
        } else
            cluster.addColumnFamily(columnFamilyDefinition);
        keyspace = HFactory.createKeyspace(ModelConfig.KEYSPACE, cluster);
    }


    public void setDataData(String workflowID, String timestamp, String eventID) {
        Mutator<String> mutator = HFactory.createMutator(keyspace, StringSerializer.get());

        mutator.addInsertion(workflowID, ModelConfig.WORKFLOW_EVENT_COLUMN_FAMILY,
                HFactory.createStringColumn(timestamp, eventID));
        mutator.execute();
    }


    /* TODO
     *
     *    add method to retrieve information
     *    public <HaspMap> getWorkflowData(String workflowID) { ... }
      * */

}
